// newaddress.js
var shihan = require('../../common/fetch')
var config = require('../../common/config')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_default: 0,
    disabled_area: false,
    address_info: '请选择省市区'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      operate_type: options.type
    })
    if (options.type == 'editor') {
      shihan.sendRequest(config.get_address, 'POST', { id: options.id })
        .then(function(response) {
          // console.log(response);
          that.setData({
            address_id: options.id,
            operate_type: options.type,
            address: response.data.data,
            address_info: response.data.data.sh_province + response.data.data.sh_city + response.data.data.sh_area,
            is_default: response.data.data.sh_is_default,
            a_id: response.data.data.sh_area_id
          })
          wx.setNavigationBarTitle({
            title: '编辑收货地址'
          })
        }, function(error) {
          console.log(error);
        });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.selectAddress = this.selectComponent("#selectAddress");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  showPopup: function () {
    // console.log(this.selectAddress);
    this.selectAddress._showPopup();
  },

  formSubmit: function (e) {
    var that = this, data = {};
    // 收货人
    var realname = e.detail.value.realname;
    // 联系电话
    var mobile = e.detail.value.mobile;
    // detail
    var detail = e.detail.value.detail;

    // 表单验证
    if (realname == '') {
      shihan.showToast.call(that, '请填写收货人');
      return;
    }
    if (!mobile) {
      shihan.showToast.call(that, '请填写正确手机号码');
      return;
    }
    if (that.data.address_info == '请选择省市区') {
      shihan.showToast.call(that, '请输入所在地区');
      return;
    }
    if (detail == '') {
      shihan.showToast.call(that, '详情地址不能为空');
      return;
    }
    
    data["mobile"] = mobile;
    data["address"] = detail;
    data["full_name"] = realname;
    data["default"] = that.data.is_default;

    if (that.data.operate_type == 'add') {
      data["a_id"] = that.data.region_id;
      shihan.sendRequest(config.add_address, 'POST', data)
        .then(function (response) {
          that.navigateBack(response, data);
          // console.log(response);
        }, function (error) {
          console.log(error);
        });
    } else if (that.data.operate_type == 'editor') {
      data["id"] = that.data.address_id;
      data["a_id"] = that.data.region_id ? that.data.region_id : that.data.a_id;
      shihan.sendRequest(config.update_address, 'POST', data)
        .then(function (response) {
          that.navigateBack(response, data);
        }, function (error) {
          console.log(error);
        });
    }
  },
  navigateBack: function (response, user_input) {
    var that = this;
    if (response.data.code == 0) {
      wx.navigateBack()
    } else {
      shihan.showToast.call(that, response.data.msg);
    }
  },
  switchChange: function (e) {
    // 是否设为默认
    this.setData({
      is_default: e.detail.value ? 1 : 0
    })
  },
  disabledArea: function (e) {
    this.setData({
      disabled_area: e.detail == 1 ? true : false
    })
  },
  changeAddress: function (e) {
    //e.detail 自定义组件触发事件时提供的detail对象
    this.setData({
      region_id: e.detail.region_id,
      regionName: e.detail.regionName,
      address_info: e.detail.address
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  }
})